{% load i18n %}
{% load image_tags %}
{% load currency_filters %}
{% load purchase_info_tags %}
{% load widget_tweaks %}

{% if basket_warnings %}
    <h5>{% trans "Important messages about items in your basket" %}</h5>
    {% for warning in basket_warnings %}
        <div class="alert alert-warning">{{ warning }}</div>
    {% endfor %}
{% endif %}

{% if upsell_messages %}
    <div class="well">
        <h3>{% trans "You could be missing out on offers!" %}</h3>
        {% for upsell in upsell_messages %}
            {% blocktrans with message=upsell.message url=upsell.offer.get_absolute_url offer_name=upsell.offer.name %}
                <div class="warning">{{ message }} to qualify for the <a href="{{ url }}">{{ offer_name }}</a> special offer</div>
            {% endblocktrans %}
        {% endfor %}
    </div>
{% endif %}

{% if not basket.is_empty %}
    {% block basket_form_headers %}
        <div class="basket-title hidden-xs">
            <div class="row">
                <h2 class="col-sm-6 h3">{% trans "Items to buy now" %}</h2>
                <p class="col-sm-3 h3">{% trans "Quantity" %}</p>
                <p class="col-sm-1 h3 align-right">{% trans "Price" %}</p>
                <p class="col-sm-2 h3 align-right">{% trans "Total" %}</p>
            </div>
        </div>
    {% endblock %}

    {% block basket_form_main %}
        <form method="post" class="basket_summary" id="basket_formset">
            {% csrf_token %}
            {{ formset.management_form }}

            {% for form in formset %}
                {% with line=form.instance product=form.instance.product %}
                    {% purchase_info_for_line request line as session %}
                    <div class="basket-items">
                        <div class="row">
                            <div class="col-sm-2">
                                {{ form.id }}
                                {% with image=product.primary_image %}
                                    {% oscar_thumbnail image.original "100x100" upscale=False as thumb %}
                                    <a href="{{ product.get_absolute_url }}">
                                        <img class="thumbnail" src="{{ thumb.url }}" alt="{{ product.get_title }}"/>
                                    </a>
                                {% endwith %}
                            </div>
                            <div class="col-sm-4">
                                <h3><a href="{{ product.get_absolute_url }}">{{ line.description }}</a></h3>
                                <p class="availability {{ session.availability.code }}">{{ session.availability.message }}</p>
                            </div>
                            <div class="col-sm-3">
                                <div class="checkout-quantity">
                                    <div class="input-group  {% if form.errors %}error{% endif %}">
                                        {% render_field form.quantity class+="form-control" %}
                                    <span class="input-group-btn">
                                        <button class="btn btn-default" type="submit" data-loading-text="{% trans 'Updating...' %}">{% trans "Update" %}</button>
                                    </span>
                                    </div>
                                </div>
                                    <div>
                                        <a href="#" data-id="{{ forloop.counter0 }}" data-behaviours="remove" class="inline">{% trans "Remove" %}</a>
                                        {% if user.is_authenticated %}
                                            | <a href="#" data-id="{{ forloop.counter0 }}" data-behaviours="save" class="inline">{% trans "Save for later" %}</a>
                                        {% endif %}
                                        <div style="display:none">
                                            {{ form.save_for_later }}
                                            {{ form.DELETE }}
                                        </div>
                                        {% for field_errors in form.errors.values %}
                                            {% for error in field_errors %}
                                                <span class="error-block"><i class="icon-exclamation-sign"></i> {{ error }}</span>
                                            {% endfor %}

                                        {% endfor %}
                                    </div>

                            </div>
                            <div class="col-sm-1">
                                {% if not line.unit_price_incl_tax and not line.unit_tax_excl_tax %}
                                    <p class="align-right">&mdash;</p>
                                {% else %}
                                    <p class="price_color align-right">
                                        {% if line.is_tax_known %}
                                            {{ line.unit_price_incl_tax|currency:line.price_currency }}
                                        {% else %}
                                            {{ line.unit_price_excl_tax|currency:line.price_currency }}
                                        {% endif %}
                                    </p>
                                {% endif %}
                            </div>
                            <div class="col-sm-2">
                                {% if not line.line_price_incl_tax and not line_price_excl_tax %}
                                    <p class="align-right">&mdash;</p>
                                {% else %}
                                    <p class="price_color align-right">
                                        {% if line.is_tax_known %}
                                            {{ line.line_price_incl_tax|currency:line.price_currency }}
                                        {% else %}
                                            {{ line.line_price_excl_tax|currency:line.price_currency }}
                                        {% endif %}
                                    </p>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                {% endwith %}
            {% endfor %}
        </form>
    {% endblock %}

    <div class="row">
        {% block vouchers %}
            {# Hide the entire section if a custom BasketView doesn't pass in a voucher form #}
            {% if voucher_form %}
                <div class="col-sm-6">
                    <div class="sub-header">
                        <h2>{% trans "Voucher/promo code" %}</h2>
                    </div>
                    <p id="voucher_form_link"><a href="#voucher" class="btn btn-default btn-full">{% trans "I have a voucher code..." %}</a></p>

                    <div id="voucher_form_container" style="display:none">
                        <form id="voucher_form" action="{% url 'basket:vouchers-add' %}" method="post">
                            {% csrf_token %}
                            {% include "oscar/partials/form_fields.html" with form=voucher_form %}
                            <button type="submit" class="btn btn-info" data-loading-text="{% trans 'Adding...' %}">{% trans "Add voucher" %}</button>
                            {% trans "or" %} <a href="#" id="voucher_form_cancel">{% trans "cancel" %}</a>
                        </form>
                    </div>
                </div>
            {% endif %}
        {% endblock vouchers %}

        {% block baskettotals %}
            <div class="col-sm-6">
                <div class="sub-header">
                    <h2>{% trans "Totals" %}</h2>
                </div>
                {% include 'oscar/basket/partials/basket_totals.html' with editable=1 %}
            </div>
        {% endblock baskettotals %}
    </div>


    {% block formactions %}
        <div class="form-group clearfix">
            <div class="row">
                <div class="col-sm-4 col-sm-offset-8">
                    <a href="{% url 'checkout:index' %}" class="btn btn-lg btn-primary btn-block">{% trans "Proceed to checkout" %}</a>
                </div>
            </div>
        </div>
    {% endblock formactions %}

{% else %}
    {% block emptybasket %}
        <p>
            {% trans "Your basket is empty." %}
            <a href="{{ homepage_url }}">{% trans "Continue shopping" %}</a>
        </p>
    {% endblock %}
{% endif %}

{% block savedbasket %}
    {% if user.is_authenticated and saved_formset %}
        <div class="well">
            <div class="sub-header">
                <h2>{% trans "Items to buy later" %}</h2>
            </div>
            <div class="row basket-title hidden-xs">
                <p class="col-sm-8 h4">{% trans "Items" %}</p>
                <p class="col-sm-2 h4 align-center">{% trans "Price" %}</p>
                <div class="col-sm-2">&nbsp;</div>
            </div>
            <form action="{% url 'basket:saved' %}" method="post" class="form-stacked later_summary" id="saved_basket_formset">
                {% csrf_token %}
                {{ saved_formset.management_form }}
                {% for form in saved_formset %}
                    {% purchase_info_for_product request form.instance.product as session %}
                    <div class="basket-items">
                        <div class="row">
                            <div class="col-sm-2">
                                {{ form.id }}
                                {% with image=form.instance.product.primary_image %}
                                    {% oscar_thumbnail image.original "100x100" upscale=False as thumb %}
                                    <a href="{{ form.instance.product.get_absolute_url }}">
                                        <img class="thumbnail" src="{{ thumb.url }}" alt="{{ form.instance.product.get_title }}">
                                    </a>
                                {% endwith %}
                            </div>
                            <div class="col-sm-6">
                                <h3><a href="{{ form.instance.product.get_absolute_url }}">{{ form.instance.description }}</a></h3>
                                <p class="availability {{ session.availability.code }}">{{ session.availability.message }}</p>
                                <a href="#" data-id="{{ forloop.counter0 }}" data-behaviours="remove">{% trans "Remove" %}</a>
                                <div style="display:none">
                                    {{ form.move_to_basket }}
                                    {{ form.DELETE }}
                                </div>
                            </div>
                            {% purchase_info_for_product request form.instance.product as saved %}
                            <div class="col-sm-2 align-center">
                                <p class="price_color">
                                    {% if saved.price.is_tax_known %}
                                        {{ saved.price.incl_tax|currency:saved.price.currency }}
                                    {% else %}
                                        {{ saved.price.excl_tax|currency:saved.price.currency }}
                                    {% endif %}
                                </p>
                            </div>
                            <div class="col-sm-2">
                                <a href="#" data-id="{{ forloop.counter0 }}" class="btn pull-right btn-block" data-behaviours="move">{% trans "Move to basket" %}</a>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </form>
        </div>
    {% endif %}
{% endblock %}
